# -*- coding: utf-8 -*-

import numpy as np

def calculate_power(data):   
    Ns=len(data) #采样点
    data_spec = np.fft.fft(np.fft.ifftshift(data))
    data_spec = data_spec / Ns   #计算真实的幅度值
    return data_spec
    
time_window=20e-12;    #采样窗口宽度20ps，该值决定了傅里叶变换后的频率分辨率
dt= 1e-15 #采样时间间隔
t = np.arange(0,time_window+dt,dt)  
df = 1.0/time_window #频率分辨率
raw_data=np.loadtxt('T_E.csv',delimiter=",")
Ns = len(raw_data)
data = calculate_power(raw_data[:,1])
single_f = np.arange(0,np.math.floor(Ns/2),1) * df
#power specturm
double_power_spec_W = np.zeros(Ns)
double_power_spec_W = np.abs(data)**2
single_power_spec_W = 2*double_power_spec_W[1:np.math.floor(Ns/2)+1] #单边功率谱，单位W
ts = 1/(299792458.0/(1*1e9)*100)*single_f*1e-9
np.savetxt('spectrum.txt',np.array([ts,10*single_power_spec_W]).T)


